home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
8bitfiles.net/archives
/
archives.tar
/
archives
/
compuserve-file-archive
/
05 Programming
/
TOWERS.SRC
< prev
next >
Wrap
Text File
|
2019-04-13
|
4KB
|
175 lines
/* hanoi -- graphic solution to the */
/* towers of hanoi puzzle */
/* copyright 1982-4 Michael M Rubenstein */
/* C-64 (Super-C) version by Don Messerli */
/* must be linked with stdio2.l */
#include "stdio.c"
/* graphics characters */
#define PINCHR '|'
#define BASECHR '-'
#define DISKCHR '*'
/* lines on screen */
#define BASELIN 17
#define CNTLIN 21
#define ASKLIN 22
/* screen width */
#define WIDTH 39
#define MAXDISK 12
int pins[MAXDISK][3];
int pinpos[3];
int ndisks, count;
/* clear screen -- change for your computer */
clear()
putchar(CLR);
|
main()
int i;
i = WIDTH/3;
pinpos[0] = WIDTH/6;
pinpos[1] = pinpos[0] + i;
pinpos[2] = pinpos[1] + i;
title();
while (ndisks = ask())
setup();
hanoi(ndisks, 0, 1, 2);
|
|
/* solve the puzzle -- move n disks */
hanoi(n, from, to, using)
int n, from, to, using;
if (n)
hanoi(n-1, from, using, to);
movedsk(from, to);
putcnt(++count);
hanoi(n-1, using, to, from);
|
|
/* setup for solution */
setup()
int i, j;
title();
cursor(BASELIN, 0);
for (i = 0; ++i <= WIDTH;)
putchar(BASECHR);
for (i = ndisks; i; --i)
for (j = 0; j < 3; ++j)
cursor(BASELIN - i, pinpos[j]);
putchar(PINCHR);
|
for (i = 0; i < ndisks; ++i)
draw(ndisks-i, BASELIN-1-i, pinpos[0]);
pins[i][0] = ndisks - i;
pins[i][1] = pins[i][2] = 0;
|
putcnt(count = 0);
|
/* draw a disk of size n at x,y */
draw(n, x, y)
int n, x, y;
draw1(DISKCHR, n, x, y);
|
/* undraw a disk of size n at x,y */
undraw(n, x, y)
int n, x, y;
draw1(' ', n, x, y);
|
/* aux function for draw & undraw */
draw1(c, n, x, y)
int c, n, x, y;
int i;
cursor(x, y - n);
for (i = n; i--;)
putchar(c);
cursor(x, y + 1);
for (i = n; i--;)
putchar(c);
|
/* move a disk */
movedsk(from, to)
int from, to;
int f, t, n, i, j;
for (f = ndisks; !pins[--f][from];) /* find top disk on from */
;
for (t = 0; pins[t][to]; ++t) /* find first free on to */
;
n = pins[f][from]; /* size of disk */
/* move it up above pins */
for (i = BASELIN - f; --i >= BASELIN - ndisks;)
draw(n, i - 1, pinpos[from]);
undraw(n, i, pinpos[from]);
|
/* move it to new pin */
if (from < to)
for (i = pinpos[from]; i < pinpos[to]; ++i)
cursor(BASELIN - ndisks - 1, i - n);
putchar(' ');
for (j = n; j--;)
putchar(DISKCHR);
putchar(' ');
for (j = n; j--;)
putchar(DISKCHR);
|
else
for (i = pinpos[from]; i > pinpos[to]; --i)
cursor(BASELIN - ndisks - 1, i - n - 1);
for (j = n; j--;)
putchar(DISKCHR);
putchar(' ');
for (j = n; j--;)
putchar(DISKCHR);
putchar(' ');
|
/* move it down */
for (i = BASELIN-1-ndisks; i < BASELIN-1-t; ++i)
draw(n, i+1, pinpos[to]);
undraw(n, i, pinpos[to]);
|
/* record new setup */
pins[t][to] = pins[f][from];
pins[f][from] = 0;
|
/* put the count to the screen */
putcnt(n)
int n;
cursor(CNTLIN, 0);
printf("Moves: %4d", n);
|
/* display title */
title()
clear();
printf("Towers of Hanoi Puzzle\n");
printf("Copyright 1982 Michael M Rubenstein\n");
printf("C-64 (Super-C) version by Don Messerli");
|
/* ask how many disks */
ask()
int n;
cursor(ASKLIN, 0);
printf("How many disks (1-6)? \b\b\b\b");
if (scanf("%d",&n) == NULL)
return 0;
if (n >= 0 && n <= 6)
return n;
|